package com.cn.jsdr.service.serviceImpl;

import com.cn.jsdr.common.SqlConfig;
import com.cn.jsdr.service.LogicService;
import com.cn.jsdr.service.RediService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class LogicServiceImpl implements LogicService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RediService rediService;
    @Override
    public Map<String, Object> expLogic(ArrayList<String> sqlIds, Map<String, Object> map) {
        Map allMap = new HashMap();
        for(String sqlid :sqlIds) {
            Map exe = new HashMap();
            Map sql = (Map)rediService.getSql(sqlid);
            String keyword = "" + sql.get("keyword");
            if (!"".equals(keyword) && keyword != null) {
                String handler = "" + sql.get("handler");
                if (!"".equals(handler) && handler != null ) {
                    Map handMap = (Map)map.get(handler);
                    exe.putAll(handMap);
                    allMap.put("sql"+handler + sqlid,handMap);
                    String exeSql;
                    if (SqlConfig.KEY_WORD_Q.equalsIgnoreCase(keyword)) {
                        exeSql = SqlConfig.getQuerySql(sql, exe);
                        List<Map<String, Object>> m =jdbcTemplate.queryForList(exeSql);
                        allMap.put("res"+handler,m);
                        System.out.println(exeSql);
                        continue;
                    } else if (SqlConfig.KEY_WORD_U.equalsIgnoreCase(keyword)) {
                        exeSql = SqlConfig.getUpdateSql(sql, exe);
                        int m =jdbcTemplate.update(exeSql);
                        allMap.put("res"+handler,m);
                        System.out.println(exeSql);
                        continue;
                    } else if (SqlConfig.KEY_WORD_A.equalsIgnoreCase(keyword)) {
                        exeSql = SqlConfig.getAddSql(sql, exe);
                        int m =jdbcTemplate.update(exeSql);
                        allMap.put("res"+handler,m);
                        System.out.println(exeSql);
                        continue;
                    } else if (SqlConfig.KEY_WORD_D.equalsIgnoreCase(keyword)) {
                        exeSql = SqlConfig.getDeletesql(sql, exe);
                        int m =jdbcTemplate.update(exeSql);
                        allMap.put("res"+handler,m);
                        System.out.println(exeSql);
                        continue;
                    } else if ("call".equalsIgnoreCase(keyword)) {
                       /*
                        exeSql = Config.getDeletesql(sql, exe);
                        jdbcTemplate.call(exeSql,"");
                        allMap.put(handler,m);
                        System.out.println(exeSql);*/
                        continue;
                    }
                    throw new RuntimeException("【关键字】错误！");
                }
                throw new RuntimeException("【参数句柄】不能为空！");
            }
            throw new RuntimeException("【关键字】不能为空！");
        }
        return allMap;
    }
}
